package com.foreveross.weixin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.foreveross.util.XmlUtil;
import com.foreveross.weixin.common.Constant;
import com.foreveross.weixin.util.StringUtil;
import com.foreveross.weixin.vo.event.BaseEvent;
import com.foreveross.weixin.vo.event.CustomMenuEvent;
import com.foreveross.weixin.vo.response.BaseMessage;
import com.foreveross.weixin.vo.response.TextMessage;
import com.foreveross.weixin.vo.response.Article;
import com.foreveross.weixin.vo.response.NewsMessage;



/** 
 * 核心服务类 
 *  
 * @author wm 
 * @date 2014-03-10 
 */
public class CoreService {
	 /** 
     * 处理微信发来的请求 
     *  
     * @param request 
     * @return 
     */  
    public static String processRequest(HttpServletRequest request) {  

        try {  
            // xml请求解析  
            Map<String, String> requestMap = XmlUtil.parseXml(request);  
            // 消息类型  
            String msgType = requestMap.get(Constant.MSG_TYPE);   
            System.out.println("requestMap.toString():"+requestMap.toString());
            //先转换成基础对象
            BaseMessage baseMessage =  StringUtil.MapToClazz(requestMap, BaseMessage.class);
            
            // 事件推送  
            if (msgType.equals(Constant.REQ_MESSAGE_TYPE_EVENT)) {
            	BaseEvent baseEvent = StringUtil.MapToClazz(requestMap, BaseEvent.class);
                // 事件类型  
            	String eventType = baseEvent.getEvent();
                // 订阅  
                if (eventType.equals(Constant.EVENT_TYPE_SUBSCRIBE)) {  
                	TextMessage textMessage = new TextMessage();
                	textMessage.setContent(getMainContent());	
                	textMessage.setCreateTime(baseMessage.getCreateTime());
                	textMessage.setFromUserName(baseMessage.getToUserName());
                	textMessage.setFuncFlag(baseMessage.getFuncFlag());
                	textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);
                	textMessage.setToUserName(baseMessage.getFromUserName());
                	
                	return XmlUtil.ClazzToXmlWithCDATA(textMessage);    
                }  
                // 取消订阅  
                else if (eventType.equals(Constant.EVENT_TYPE_UNSUBSCRIBE)) {  
                    // 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息  
                }  
                // 自定义菜单点击事件  
                else if (eventType.equals(Constant.EVENT_TYPE_CLICK)) {  
                    // 自定义菜单权没有开放，暂不处理该类消息  
                	CustomMenuEvent customMenuEvent = StringUtil.MapToClazz(requestMap, CustomMenuEvent.class);
                	String eventKey = customMenuEvent.getEventKey();
                	
                	// 创建图文消息    
                    NewsMessage newsMessage = new NewsMessage();  
                    newsMessage.setToUserName(baseMessage.getFromUserName());  
                    newsMessage.setFromUserName(baseMessage.getToUserName());  
                    newsMessage.setCreateTime(new Date().getTime());  
                    newsMessage.setFuncFlag(baseMessage.getFuncFlag());  
                    newsMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_NEWS);
                	
                    List<Article> articleList = new ArrayList<Article>();
                      
                    // 设置图文消息个数  
                    newsMessage.setArticleCount(articleList.size());  
                    // 设置图文消息包含的图文集合  
                    newsMessage.setArticles(articleList);  
                    // 将图文消息对象转换成xml字符串  
                    return XmlUtil.ClazzToXmlWithCDATA(newsMessage,new Article()); 
                }  
            }else{
            	TextMessage textMessage = new TextMessage();
            	textMessage.setContent(getMainContent());
            	textMessage.setCreateTime(baseMessage.getCreateTime());
            	textMessage.setFromUserName(baseMessage.getToUserName());
            	textMessage.setFuncFlag(baseMessage.getFuncFlag());
            	textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);
            	textMessage.setToUserName(baseMessage.getFromUserName());
            	
            	//textMessage.setContent(respContent);
            	return XmlUtil.ClazzToXmlWithCDATA(textMessage);  
            }
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;
    }  
     
    
    
    /** 
     * 主菜单
     *  
     * @return 
     */  
    public static String getMainContent() {  
        StringBuffer buffer = new StringBuffer();  
        buffer.append("华润置地 ").append("\n\n");  
        buffer.append("深圳大区物业公司 ").append("\n\n");  
        buffer.append("欢迎您！ ").append("\n\n");  

        return buffer.toString();  
    } 
    
}
